import 'package:froom/froom.dart';

/// 产品实体 - 测试用
@Entity(tableName: 'products')
class Product {
  @PrimaryKey(autoGenerate: true)
  final int? id;

  final String name;

  final String description;

  final double price;

  @ColumnInfo(name: 'category_id')
  final int categoryId;

  @ColumnInfo(name: 'is_active')
  final bool isActive;

  final String? tags; // JSON array as string

  @ColumnInfo(name: 'created_at')
  final DateTime? createdAt;

  const Product({
    this.id,
    required this.name,
    required this.description,
    required this.price,
    required this.categoryId,
    this.isActive = true,
    this.tags,
    this.createdAt,
  });

  Product copyWith({
    int? id,
    String? name,
    String? description,
    double? price,
    int? categoryId,
    bool? isActive,
    String? tags,
    DateTime? createdAt,
  }) {
    return Product(
      id: id ?? this.id,
      name: name ?? this.name,
      description: description ?? this.description,
      price: price ?? this.price,
      categoryId: categoryId ?? this.categoryId,
      isActive: isActive ?? this.isActive,
      tags: tags ?? this.tags,
      createdAt: createdAt ?? this.createdAt,
    );
  }

  @override
  bool operator ==(Object other) =>
      identical(this, other) ||
      other is Product &&
          runtimeType == other.runtimeType &&
          id == other.id &&
          name == other.name &&
          description == other.description &&
          price == other.price &&
          categoryId == other.categoryId &&
          isActive == other.isActive;

  @override
  int get hashCode =>
      id.hashCode ^
      name.hashCode ^
      description.hashCode ^
      price.hashCode ^
      categoryId.hashCode ^
      isActive.hashCode;

  @override
  String toString() {
    return 'Product{id: $id, name: $name, price: $price, categoryId: $categoryId, isActive: $isActive, createdAt: $createdAt}';
  }

  static String getTableName() => 'products';
  static List<String> getPrimaryKey() => ['id'];
  static Map<String, String> getFieldCol() => {
        'id': 'id',
        'name': 'name',
        'description': 'description',
        'price': 'price',
        'categoryId': 'category_id',
        'isActive': 'is_active',
        'tags': 'tags',
        'createdAt': 'created_at',
      };
  static Product toEntity(Map<String, Object?> map) => Product(
        id: map['id'] as int?,
        name: map['name'] as String,
        description: map['description'] as String,
        price: map['price'] as double,
        categoryId: map['category_id'] as int,
        isActive: (map['is_active'] as int) == 1,
        tags: map['tags'] as String?,
        createdAt: map['created_at'] != null
            ? DateTime.parse(map['created_at'] as String)
            : null,
      );
  static Map<String, Object?> toMap(Product product) => {
        'id': product.id,
        'name': product.name,
        'description': product.description,
        'price': product.price,
        'category_id': product.categoryId,
        'is_active': product.isActive ? 1 : 0,
        'tags': product.tags,
        'created_at': product.createdAt?.toIso8601String(),
      };
}
